import os
import pytest
import requests
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Base configuration
BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:8080')
API_PREFIX = '/api'

@pytest.fixture(scope="session")
def base_url():
    """Return the base URL for API requests."""
    return f"{BASE_URL}{API_PREFIX}"

@pytest.fixture(scope="session")
def auth_token():
    """Get authentication token for API requests."""
    # TODO: Implement actual OAuth2 token acquisition
    # This is a placeholder - you'll need to implement the actual OAuth2 flow
    return "your-auth-token"

@pytest.fixture(scope="session")
def headers(auth_token):
    """Return headers for API requests."""
    return {
        "Authorization": f"Bearer {auth_token}",
        "Content-Type": "application/json"
    }

@pytest.fixture(scope="function")
def api_client(base_url, headers):
    """Create an API client with base URL and headers."""
    class APIClient:
        def __init__(self, base_url, headers):
            self.base_url = base_url
            self.headers = headers

        def get(self, endpoint, params=None):
            return requests.get(f"{self.base_url}{endpoint}", headers=self.headers, params=params)

        def post(self, endpoint, data=None):
            return requests.post(f"{self.base_url}{endpoint}", headers=self.headers, json=data)

        def post_update(self, endpoint, data=None):
            """POST method for update operations."""
            return requests.post(f"{self.base_url}{endpoint}/update", headers=self.headers, json=data)

        def post_delete(self, endpoint, data=None):
            """POST method for delete operations."""
            return requests.post(f"{self.base_url}{endpoint}/delete", headers=self.headers, json=data)

    return APIClient(base_url, headers) 